package com.yzx.lifeassistants.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.haarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.yzx.lifeassistants.R;
import com.yzx.lifeassistants.adapter.ActivityAdapter;
import com.yzx.lifeassistants.base.BaseActivity;
import com.yzx.lifeassistants.bean.ActivityInfo;
import com.yzx.lifeassistants.model.IQueryActivity;
import com.yzx.lifeassistants.model.callback.IQueryActivityCB;
import com.yzx.lifeassistants.model.impl.QueryActivityImpl;
import com.yzx.lifeassistants.utils.ToastUtils;
import com.yzx.lifeassistants.view.widget.CircularLoadingDialog;

/**
 * @Description: 活动资讯
 * @author: yzx
 * @time: 2016-3-7 下午1:27:01
 */
public class ActivityActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener {
	private ImageButton backBtn;// 返回按钮
	private ListView activityLV;// 通知列表
	private PullToRefreshListView activityPTRLV;// 刷新
	private ILoadingLayout activityILL;// 加载

	private Handler handler;
	private IQueryActivityCB queryCallBack;// 回调
	private IQueryActivity queryActivity;// 查询
	private CircularLoadingDialog dialog;// 加载
	private static final int STATE_REFRESH = 0;// 下拉刷新
	private static final int STATE_MORE = 1;// 加载更多
	private int curPage = 0; // 当前页的编号，从0开始
	private int loadType;// 加载类型
	private List<ActivityInfo> dataList;// 图书列表
	private ActivityAdapter adapter;// 通知列表适配器

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity);
		initView();
		setListener();
		initData();
	}

	/**
	 * 
	 * @Description: 初始化控件
	 */
	private void initView() {
		backBtn = (ImageButton) findViewById(R.id.activity_back_btn);
		activityPTRLV = (PullToRefreshListView) findViewById(R.id.activity_listview);
		activityILL = activityPTRLV.getLoadingLayoutProxy();
		activityILL.setLastUpdatedLabel("");
		activityILL.setPullLabel("继续拖动");
		activityILL.setRefreshingLabel("正在装载数据···");
		activityILL.setReleaseLabel("放开装载更多");
		activityLV = activityPTRLV.getRefreshableView();
	}

	/**
	 * 
	 * @Description: 设置监听
	 */
	private void setListener() {
		backBtn.setOnClickListener(this);
		// 滑动监听
		activityPTRLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if (firstVisibleItem == 0) {
					activityILL.setLastUpdatedLabel("");
					activityILL.setPullLabel("下拉刷新");
					activityILL.setRefreshingLabel("加载中···");
					activityILL.setReleaseLabel("释放刷新");
				} else if (firstVisibleItem + visibleItemCount == totalItemCount) {
					activityILL.setLastUpdatedLabel("");
					activityILL.setPullLabel("继续拖动");
					activityILL.setRefreshingLabel("正在装载数据···");
					activityILL.setReleaseLabel("放开装载更多");
				}
			}
		});
		// 下拉刷新监听
		activityPTRLV.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				// 下拉刷新(从第一页开始装载数据)
				queryActivity(0, STATE_REFRESH);
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				// 上拉加载更多(加载下一页数据)
				queryActivity(curPage, STATE_MORE);
			}

		});
		activityLV.setOnItemClickListener(this);
	}

	/**
	 * 
	 * @Description: 初始化数据
	 */
	private void initData() {
		dialog = new CircularLoadingDialog(this);
		dataList = new ArrayList<ActivityInfo>();
		adapter = new ActivityAdapter(this, dataList);
		SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(
				adapter);
		swingBottomInAnimationAdapter.setAbsListView(activityLV);
		activityLV.setAdapter(swingBottomInAnimationAdapter);
		handler = new Handler();
		queryCallBack = new IQueryActivityCB() {

			@Override
			public void querySuccess(final List<ActivityInfo> infos) {
				handler.post(new Runnable() {

					@Override
					public void run() {// 查询成功
						dialog.dismiss();
						if (STATE_REFRESH == loadType) {
							dataList.clear();
							curPage = 0;
						}
						dataList.addAll(infos);
						curPage++;
						activityPTRLV.onRefreshComplete();
						adapter.notifyDataSetChanged();
					}
				});
			}

			@Override
			public void queryError(final int code) {
				handler.post(new Runnable() {

					@Override
					public void run() {
						switch (code) {
						case IQueryActivityCB.RESULR_NULL: {// 暂无更多
							if (STATE_MORE == loadType) {
								showToast("暂时还未有更多的活动预告~", true);
							}
							if (STATE_REFRESH == loadType) {
								showToast("暂时还未有活动预告~", true);
							}
							break;
						}
						case IQueryActivityCB.QUERY_ERROR: {// 获取失败
							showToast("获取失败，请确认网络连接后刷新重试~", false);
							break;
						}
						default:
							break;
						}
						dialog.dismiss();
						activityPTRLV.onRefreshComplete();
						adapter.notifyDataSetChanged();
					}

				});
			}
		};
		queryActivity = new QueryActivityImpl(this, queryCallBack);
		queryActivity(0, STATE_REFRESH);
	}

	/**
	 * 
	 * @Description: 查询教务处通知
	 */
	private void queryActivity(int page, int actionType) {
		loadType = actionType;
		dialog.show();
		queryActivity.queryActivity(page + 1);
	}

	/**
	 * 
	 * @Description: 按键监听
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.activity_back_btn: {// 返回
			finish();
			break;
		}
		default:
			break;
		}
	}

	/**
	 * 
	 * @Description: 列表按键监听
	 */
	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long arg3) {
		Intent intent = new Intent(this, ActivityInfoActivity.class);
		intent.putExtra("ACTIVITY_INFO_URL", dataList.get(position - 1)
				.getUrl());
		intent.putExtra("ACTIVITY_INFO_TITLE", dataList.get(position - 1)
				.getTitle());
		startActivity(intent);
	}

	/**
	 * 
	 * @Description: Toast显示信息
	 */
	private void showToast(String message, Boolean isInfo) {
		if (isInfo) {
			ToastUtils.showInfoToast(this, message, R.id.activity_top_ll);
		} else {
			ToastUtils.showAlertToast(this, message, R.id.activity_top_ll);
		}

	}
}
